<template>
    <div class="samebox">
        <div class="q_xxpxjl">
            <img src="../assets/xxpxjl.png" alt="">
            <div style="margin-left: 120px;"> 学习培训经历</div>
        </div>
        <div class="q_zssx" style="text-align: right">
            <el-row>
                <el-button type="primary" size="medium" class="el-icon-s-promotion" @click="dialogVisible=true">新增
                </el-button>
                <el-popconfirm
                        confirm-button-text='好的'
                        cancel-button-text='不用了'
                        icon="el-icon-info"
                        icon-color="red"
                        title="您确定要删除选中的数据"
                        @confirm="removeStudy()"
                >
                    <el-button type="" size="medium" class="el-icon-delete" slot="reference" style="margin-left: 10px">删除</el-button>
                </el-popconfirm>


            </el-row>
        </div>

        <div>
            <table class="table table-bordered">
                <thead>
                <tr class="q_xuhao">
                    <td>序号</td>
                    <td>起始时间</td>
                    <td>结束时间</td>
                    <td>专业或主要内容</td>
                    <td>学习地点</td>
                    <td>证明人</td>
                    <td>培训类型</td>
                </tr>
                </thead>
                <tbody v-if="studyData.length>0">
                <tr v-for="(item,index) in studyData">
                    <td>
                        <input type="checkbox" name="chk1" @change="selectItme(item.id)">
                        <span>{{index+1}}</span>
                    </td>
                    <td>{{item.fqssj}}</td>
                    <td>{{item.fjssj}}</td>
                    <td>{{item.fzyhzynr}}</td>
                    <td>{{item.fxxdd}}</td>
                    <td>{{item.fzmr}}</td>
                    <td>{{item.fpxlx}}</td>
                </tr>

                </tbody>
                <tr v-else>
                    <td colspan="7">
                        <el-empty></el-empty>
                    </td>
                </tr>
            </table>

        </div>

        <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="30%"
                center>
            <!--  -->
            <el-form :model="studyRule" :rules="rules" ref="studyRule" label-width="100px" class="demo-ruleForm">
                <el-form-item label="工作名称" required prop="fzyhzynr">
                    <el-input v-model="studyRule.fzyhzynr" placeholder="学习培训专业或者内容"></el-input>
                </el-form-item>
                <el-form-item label="起始时间" required>
                    <el-form-item prop="fqssj">
                        <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd"
                                        v-model="studyRule.fqssj"
                                        style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-form-item>
                <el-form-item label="结束时间" required>
                    <el-form-item prop="fjssj">
                        <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd"
                                        v-model="studyRule.fjssj"
                                        style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-form-item>
                <el-form-item label="学习地点" prop="fxxdd">
                    <el-input v-model="studyRule.fxxdd" placeholder="学习地点"></el-input>
                </el-form-item>
                <el-form-item label="证明人" prop="fzmr">
                    <el-input v-model="studyRule.fzmr" placeholder="证明人"></el-input>
                </el-form-item>
                <el-form-item label="培训类型" prop="fpxlx">
                    <el-input v-model="studyRule.fpxlx" placeholder="培训类型"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('studyRule')">提交</el-button>
                    <el-button @click="resetForm('studyRule')">重置</el-button>
                </el-form-item>
            </el-form>
            <!---->
        </el-dialog>

        <div class="q_sub">
            <el-pagination
                    background
                    layout="prev, pager, next"
                    :current-page.sync="currentPage"
                    :hide-on-single-page="hidePage"
                    :page-size="pageSize"
                    :total="total"
                    @current-change="queryStudyInfo">
            </el-pagination>
        </div>
    </div>

</template>

<script>
    import "../assets/samecss/same.css"

    export default {

        created() {
            this.queryStudyInfo();
        },
        methods: {

            //选中数据
            selectItme(id) {
                let index = this.ids.indexOf(id);
                if (index > -1) {
                    this.ids.splice(index, index + 1)
                } else {
                    this.ids.push(id)
                }
            },
            //下一步
            next() {
                this.$router.push({path: "/work"})
            },

            //提交表单
            submitForm(formName) {
                let that = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        that.$http.post("/study/studyadd", that.studyRule).then(res => {
                            that.$message.success("添加成功");
                            that.queryStudyInfo();
                            // that.resetForm(formName);
                        })
                    } else {
                        return false;
                    }
                });
            },
            //重置表单
            resetForm(formName) {
                this.studyRule = {};
                this.$refs[formName].clearValidate();
            },
            //查询用户学习培训经历
            queryStudyInfo() {
                let that = this
                that.$http.get(`/study/studyslt?currentPage=${that.currentPage}&pageSize=${that.pageSize}`).then(res => {
                    that.currentPage = res.data.currentPage
                    that.studyData = res.data.list
                    that.total = res.data.total
                })

            },
            //移除培训经历
            removeStudy() {
                let that = this
                if (that.ids.length === 0) {
                    that.$message.warning("您还没有选择要删除的数据")
                    return
                }
                this.$http.post("/study/studydle", this.ids).then(res => {
                    that.$message.success('删除成功')
                    that.queryStudyInfo();
                })
            }

        },
        data() {
            const validateEndTime = (rule, value, callback) => {
                if (value === '' || value == null) {
                    callback(new Error('请选择日期'))
                } else {
                    if (
                        new Date(this.studyRule.fqssj).getTime() >=
                        new Date(this.studyRule.fjssj).getTime()
                    ) {
                        callback(new Error('截止时间必须大于开始时间！'))
                    } else {
                        callback()
                    }
                }
            };
            return {
                //分页参数
                currentPage: 1,
                pageSize: 10,
                hidePage:true,
                total:1,
                ids: [],  //选中数据编号
                //弹出框显示隐藏
                dialogVisible: false,
                //查询数据
                studyData: [],
                //新增数据
                newStudyData: [],

                //  规则匹配
                studyRule: {
                    fqssj: '',
                    fjssj: '',
                    fzyhzynr: '',
                    fxxdd: '',
                    fzmr: '',
                    fpxlx: '',
                },
                //element验证
                rules: {
                    fqssj: [
                        {required: true, message: '请选择开始日期', trigger: 'blur'}
                    ],
                    fjssj: [
                        {validator: validateEndTime, required: true, trigger: 'blur'}
                    ],
                    fzyhzynr: [
                        {required: true, message: '工作名称（项目、课题）', trigger: 'blur'},
                        {min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur'}
                    ],
                    fxxdd: [
                        {required: true, message: '添加学习地点', trigger: 'blur'},
                        {min: 3, max: 200, message: '长度在 3 到 200 个字符', trigger: 'blur'}
                    ],
                    fzmr: [
                        {required: true, message: '证明人', trigger: 'blur'},
                        {min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur'}
                    ],
                    fpxlx: [
                        {required: true, message: '培训类型', trigger: 'blur'},
                        {min: 3, max: 50, message: '最少 3 个字符', trigger: 'blur'}
                    ],
                },

            }
        }
    }
</script>

<style scoped>

</style>
